package com.glorysoft.tonglingxjpda.ui.order.viewmodel

import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseViewModel
import com.glorysoft.tonglingxjpda.entity.OrderListEntity
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.model.TaskRepository
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.text.SimpleDateFormat
import java.util.*

/**
 * FileName: OrderListViewModel
 * Author: XJH
 * Date: 2021/7/13 11:51
 * Description:
 * History:
 */
class OrderListDLViewModel : BaseViewModel() {

    val orderListTitle = arrayListOf(
            "订单编号",
            "订单分类",
            "箱号",
            "装置IPN",
            "PO",
            "订单LOT",
            "部件数量",
            "报废总数量",
            "已完成总数量",
            "创建时间",
            "刻印好完成",
            "订单状态",
            "订单标记"
    )

    var orderInfoEntity = MutableLiveData<OrderListEntity.OrderInfoEntity>()
    private val orderRepository = TaskRepository()
    var orderClaesList = arrayListOf<String>()

    private val onOrderClick = object : OrderListViewModel.OnClickListener {
        override fun onChildClick(entity: OrderListEntity.OrderInfoEntity) {
            val bundle = Bundle()
            bundle.putString("DOCID", entity.DOCID)
            defUI.startActivity.value = bundle
        }

    }

    //order 批次列表
    var orderItems = ObservableArrayList<OrderListEntity.OrderInfoEntity>()
    var orderBinding =
            ItemBinding.of<OrderListEntity.OrderInfoEntity>(BR.orderDlItem, R.layout.item_order_list_dl)
                    .bindExtra(BR.orderDlClick, onOrderClick)

    fun init() {
        orderInfoEntity.value = OrderListEntity.OrderInfoEntity()
        getOrderClass()
    }


    private fun getOrderClass() {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "DDFL")
        launchOnlyResult({
            val orderClassList = orderRepository.getOrderClassList(map)
            orderClaesList.clear()
            for (customInfoEntity in orderClassList.BASEINFOLIST) {
                orderClaesList.add(customInfoEntity.KEY + "#" + customInfoEntity.DESCRIPTION)
            }
        })
    }

    fun searchOrder(createDate: String, outDate: String, orderClasses: String) {
//        if (createDate.isNotEmpty()){
//            if (!compareDate(createDate,FreeApi_DateUtils.getNowStringDate("yyyy-MM-dd")))return
//        }

        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "POS")
        map.addProperty("DOCID", orderInfoEntity.value!!.DOCID)
        map.addProperty("CUSTOMERORDER", orderInfoEntity.value!!.CUSTOMERORDER)
        map.addProperty("RESERVED11", orderClasses)
        map.addProperty("RESERVED6", orderInfoEntity.value!!.RESERVED6)
        map.addProperty("PARTNAME", orderInfoEntity.value!!.PARTNAME)
        map.addProperty("CUSTOMERPARTID", orderInfoEntity.value!!.CUSTOMERPARTID)

        var time1 = "${createDate};${FreeApi_DateUtils.getNowStringDate("yyyy-MM-dd")}"
        map.addProperty(
                "CREATED",
                time1
        )


        var time2 = "${outDate};${FreeApi_DateUtils.getNowStringDate("yyyy-MM-dd")}"
        map.addProperty(
                "PLANENDDATE",
                time2
        )

//        map.addProperty("CUSTOMERCODE", orderInfoEntity.value!!.CUSTOMERCODE)
//        map.addProperty("GRADE1", orderInfoEntity.value!!.GRADE1)
//        map.addProperty("GRADE2", orderInfoEntity.value!!.GRADE2)
//        map.addProperty("RESERVED2", orderInfoEntity.value!!.RESERVED2)
        launchOnlyResult({
            val orderInfo = orderRepository.getOrderInfo(map)
            orderItems.clear()
            orderItems.addAll(orderInfo.POS)
        })
    }

    fun compareDate(beginDate: String, endDate: String): Boolean {
        val df = SimpleDateFormat("yyyy-MM-dd")

        try {
            val dt1 = df.parse(beginDate)
            val dt2 = df.parse(endDate)

            val days = ((dt2.time - dt1.time) / (1000 * 3600 * 24))
            if (days > 1) {
                defUI.toastEvent.value = ToastMessageBean("只能查询最近一天的项目！", false)
                return false
            }
            return true

        } catch (var7: Exception) {
            var7.printStackTrace()
            return false
        }
    }

    fun getStringDate(): String {
        val c = Calendar.getInstance()
        val formatter = SimpleDateFormat("yyyy-MM-dd")
        c.add(Calendar.DATE, -3)
        return formatter.format(c.time)
    }
}